package com.yiwei.ydd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.ydd.R;
import com.yiwei.ydd.api.model.PaymentListModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class PayWayListAdapter extends ItemAdapter<PaymentListModel.DatasBean, PayWayListAdapter.DefaultHolder> {


    private Context context;
    private static final String WX_APP = "WX_APP";
    private static final String ALI_APP = "ALI_APP";
    public PayWayListAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_pay_way_list, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        PaymentListModel.DatasBean item = getItem(position);
//        if (item.class_name.equals("ACCOUNT")) {
//            holder.txtName.setText(item.name + " (" + PreferenceUtils.getString(context, "balance", "0") + "元可用)");
//        } else {
//            holder.txtName.setText(item.name);
//        }
        holder.txtName.setText(item.name);
//        Glide.with(context).load(item.icon).into(holder.imgIcon);
        if (item.isCheck) {
            holder.cbCheck.setImageResource(R.mipmap.img_oil_change_check);
        } else {
            holder.cbCheck.setImageResource(R.mipmap.img_oil_change_uncheck);
        }

        if(item.en_name.equals(WX_APP)){
            holder.txtDesc.setText("亿万用户的选择，更快更安全");
        }else if(item.en_name.equals(ALI_APP)){
            holder.txtDesc.setText("快捷安全，可支持银行卡支付");
        }else {
            holder.txtDesc.setText("");
        }

        Glide.with(context).load(item.icon).into(holder.imgIcon);

        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.name, item.en_name, item.id);
                }
                for (int i = 0; i < getItemCount(); i++) {
                    if (i == position) {
                        getmSource().get(i).isCheck = true;
                    } else {
                        getmSource().get(i).isCheck = false;
                    }
                    notifyDataSetChanged();
                }
            }
        });
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String name, String en_name, String id);
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.cb_check)
        ImageView cbCheck;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        @BindView(R.id.txt_desc)
        TextView txtDesc;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void clearCheck() {
        for (int i = 0; i < getItemCount(); i++) {
            getmSource().get(i).isCheck = false;
        }
        notifyDataSetChanged();
    }

}
